$(function(){
	var page=0;
	var pageMax=100;
    //点击设置楼层时弹出遮罩层
    setFloor=function(floor){
		page=0;
		getFloorInformaion(floor);
		getDormitoryListPage(floor);
		getDormitoryList(floor,page);
		$("#dormitory_checkbox_all").prop("checked",false);
	};
	//显示遮罩层
	function modaldemo(){
		$("#modal-demo").modal("show");
	}

	//上一页
	pageTop=function(){
		if(page-1>=0){
			page--;
			getDormitoryList($("#dormitory_floor").val(),page);
			$("#dormitory_page").val(page);
		}
	}
	//下一页
	pageNext=function(){
		if(page+1<pageMax){
			page++;
			getDormitoryList($("#dormitory_floor").val(),page);
			$("#dormitory_page").val(page);
		}	
	}
	//选页
	$("#dormitory_page").change(function(){
		page=$("#dormitory_page").val();
		getDormitoryList($("#dormitory_floor").val(),page);
	});
	//更改楼层
	$("#dormitory_floor").change(function(){
		setFloor($("#dormitory_floor").val());
		//alert("");
	});

	//查找
	$("#btn-select").click(function(){
		select=new Object();
		select.id=$("#id").val();
		select.floor=$("#dormitory_floor").val();
		select.dormitoryID=$("#dormitory_id").val();
		jsonString=JSON.stringify(select);
		//alert(jsonString);
		$.post("./php/apartment-edit.php",{action:5,data:jsonString},function(jsonString){
			dormitoryObject=JSON.parse(jsonString);
			//alert(jsonString);
			switch(dormitoryObject.code){
				case '2001':
					if(dormitoryObject.message==false){
						layer.msg('未查找到该宿舍');
					}else{
						displayDormitory(dormitoryObject.message);
					}
				break;
				case '2004':
				break;
			}
		});
	});
	
	//修改单个宿舍的设置
	saveDormitory=function(dormitoryID){
		dormitoryObject=new Object();
		apartment=new Object();
		
		apartment.id=$("#id").val();
		dormitoryObject.apartment=apartment;

		dormitoryObject.dormitory=new Array();
		dormitoryObject.dormitory[0]=getDormitorySetting(dormitoryID,2);

		setDormitorySetting(dormitoryObject);
	}

	//修改选中的宿舍的设置
	$("#btn-dormitory_alter").click(function(){
		dormitoryObject=new Object();
		apartment=new Object();

		apartment.id=$("#id").val();
		dormitoryObject.apartment=apartment;

		dormitoryObject.dormitory=new Array();
		$(':checkbox[name=dormitory_checkbox]:checked').each(function(index,e){
			dormitoryObject.dormitory[index]=getDormitorySetting($(this).val(),1);
		});

		if(dormitoryObject.dormitory.length>0){
			setDormitorySetting(dormitoryObject);
		}else{
			layer.msg('未选中任何项');
		}
		
	});

	//获取单个宿舍设置,action 1-修改选中多个，2-修改单个
	function getDormitorySetting(dormitoryID,action){
		dormitorySetting=new Object();
		dormitorySetting.id=dormitoryID;
		
		
		switch(action){
			case 1:
				dormitorySetting.property=$("#dormitory_property").val();
				dormitorySetting.beds=$("#dormitory_beds").val();
				dormitorySetting.status=$("#dormitory_status").val();
				dormitorySetting.remarks=$("#dormitory_remarks").val();
			break;
			case 2:
				dormitorySetting.property=$("#table_property"+dormitoryID).val();
				dormitorySetting.beds=$("#table_beds"+dormitoryID).val();
				dormitorySetting.status=$("#table_status"+dormitoryID).val();
				dormitorySetting.remarks=$("#table_remarks"+dormitoryID).val();
			break;
			default:break;
		}

		return dormitorySetting;
	}

	//提交修改项
	function setDormitorySetting(dormitoryObject){
		dormitoryJSON=JSON.stringify(dormitoryObject);
		//alert(dormitoryJSON);
		$.post("./php/apartment-edit.php",{action:6,data:dormitoryJSON},function(jsonString){
			respondObject=JSON.parse(jsonString);
			respondObject.message;
			switch(respondObject.code){
				case '2001':
					layer.msg('修改成功');
					setFloor($("#dormitory_floor").val());
				break;
				case '2003':break;
				case '2004':
					layer.msg('修改失败，请重新提交',2000);
				break;
			}
		});
	}
	/*//获取宿舍信息 列表
	function getDormitoryInformation(floor){
		var dataObj=new Object();
		dataObj.id=1;
		dataObj.floor=floor;
		dataObj.page=1; 
        $.post("./php/apartment-edit.php",{action:4,data:JSON.stringify(dataObj)},function(jsonString){
			dormitoryObject=JSON.parse(jsonString);
			//alert(jsonString);
			switch(dormitoryObject.code){
				case '2001':
				displayDormitory(dormitoryObject.message);
				break;
				case '2003':break;
				case '2004':break;
			}
        });
	}*/

	//获取最大页数
	function getDormitoryListPage(floor){
		var dataObject=new Object();
		dataObject.id=$("#id").val();
		dataObject.floor=floor;
		dataJson=JSON.stringify(dataObject);
		$.post("./php/apartment-edit.php",{action:7,data:dataJson},function(jsonString){
			dormitoryObject=JSON.parse(jsonString);
			switch(dormitoryObject.code){
				case '2001':
				pageMax=dormitoryObject.message;
				setSelectPage(pageMax);
				break;
				case '2003':break;
				case '2004':break;
			}
        });
	}
	//获取宿舍列表
	function getDormitoryList(floor,myPage){
		var dataObject=new Object();
		dataObject.id=$("#id").val();
		dataObject.floor=floor;
		dataObject.page=myPage;
		dataJson=JSON.stringify(dataObject);
        $.post("./php/apartment-edit.php",{action:4,data:dataJson},function(jsonString){
			dormitoryObject=JSON.parse(jsonString);
			
			switch(dormitoryObject.code){
				case '2001':
					displayDormitory(dormitoryObject.message);
					modaldemo();
				break;
				case '2003':break;
				case '2004':break;
			}
        });
	}
	
	//设置页数
	function setSelectPage(listPage){
		var selectString="<option value=\"0\">1</option>";
		for(var i=1; i<listPage ;i++){
			selectString+="<option value=\""+i+"\">"+(i+1)+"</option>";
		}

		$("#dormitory_page").html(selectString);
		$("#dormitory_page").val(0);
	}

	//获取单层信息
	function getFloorInformaion(floor){
		var dataObj=new Object();
		dataObj.id=$("#id").val();;
		dataObj.floor=floor;
        $.post("./php/apartment-edit.php",{action:3,data:JSON.stringify(dataObj)},function(jsonString){
			jsonObject=JSON.parse(jsonString);
			floorObject=jsonObject.message;
			//alert(jsonString);
			switch(jsonObject.code){
				case '2001':
				var select_str="";
				for(var i=1; i <= parseInt($("#floor").val()) ;i++){
					select_str+="<option value=\""+i+"\" >"+i+"</option>";
				}
				$("#dormitory_floor").html(select_str);
				$("#dormitory_floor").val(floorObject.floor);
				$("#dormitory_beds").val(floorObject.beds);
				$("#dormitory_status").val(floorObject.status);

				switch(floorObject.property){
					case 1:
						$("#dormitory_property").html("<option value=\"1\" selected=\"\">女生</option>");
					break;
					case 2:
						$("#dormitory_property").html("<option value=\"2\" selected=\"\">男生</option>");
					break;
					case 3:
						$("#dormitory_property").html("<option value=\"1\" selected=\"\">女生</option>"+"<option value=\"2\">男生</option>");
					break;
					default:
					break;
				}
				break;
				case '2004':break;
				default:break;
			}
			
        });
	}


    //显示宿舍信息
	function displayDormitory(obj){
		var str="";
		$.each(obj, function(e, dormitory) {
			str+=handleTable(dormitory);
		});
		$("#display-table").html(str);
    }
	
	function setDormitoryInformation(){
		
	}
    //处理表格
	function handleTable(obj){
		var str="<tr class=\"text-c\"><td><input name=\"dormitory_checkbox\" type=\"checkbox\" value=\""+obj.dormitory_id+"\"></td>"+
		"<td id=\"table_apartment_id"+obj.dormitory_id+"\">"+obj.id+"</td><td id=\"table_name"+obj.dormitory_id+"\">"+$("#name").val()+"</td><td id=\"table_floor"+obj.dormitory_id+"\">"+
		obj.floor+"</td><td id=\"table_dormitory_id"+obj.dormitory_id+"\">"+obj.dormitory_id+"</td>"+
		"<td><input id=\"table_beds"+obj.dormitory_id+"\" type=\"number\" value=\""+obj.beds+"\" class=\"input-text text-c\" style=\"width:100%\"></td>"+
		"<td><select id=\"table_property"+obj.dormitory_id+"\" class=\"select\" size=\"1\" style=\"height:29px;\">";
		switch(parseInt(obj.property)){
			case 1:
			str+="<option value=\"1\" selected=\"\">女生</option></td>";
			break;
			case 2:
			str+="<option value=\"2\" selected=\"\">男生</option></td>";
			break;
			case 3:
			str+="<option value=\"1\" selected=\"\">女生</option>"+
				"<option value=\"2\">男生</option></td>";
			break;
			default:
			str+="<option value=\"1\" selected=\"\">女生</option>"+
				"<option value=\"2\">男生</option></td>";
			break;
		}
		str+="<td><select id=\"table_status"+obj.dormitory_id+"\" class=\"select\" size=\"1\" style=\"height:29px;\">";
		switch(parseInt(obj.status)){
			case 0:
			str+="<option value=\"1\">使用</option>"+
			"<option value=\"2\" selected=\"\">停用</option></select></td>";
			break;
			case 1:
			str+="<option value=\"1\" selected=\"\">使用</option>"+
			"<option value=\"2\">停用</option></select></td>";
			break;
			case 2:
				str+="<option value=\"1\" selected=\"\">使用</option>";
			break;
			case 3:
				str+="<option value=\"1\" selected=\"\">使用</option>";
			break;
			default:
			str+="<option value=\"1\" selected=\"\">使用</option>"+
			"<option value=\"2\">停用</option></select></td>";
			break;
		}
		
		str+="<td><input id=\"table_remarks"+obj.dormitory_id+"\" type=\"text\" class=\"input-text\" value=\""+obj.remarks+"\" placeholder=\"\"></td>"+
		"<td><a onClick=\"saveDormitory("+obj.dormitory_id+")\" style=\"text-decoration:none\" class=\"ml-5\" href=\"javascript:;\" title=\"保存\">"+
		"<i class=\"Hui-iconfont\">&#xe632;</i></a></td></tr>";

		return str;
	}
});